package main

import (
	"fmt"
	"sort"
)

func main() {
	//数组初始化
	var arr1 [5]int
	for i := 0; i < len(arr1); i++ {
		arr1[i] = i * 2
	}
	for i := 0; i < len(arr1); i++ {
		fmt.Printf("Array at index %d is %d\n", i, arr1[i])
	}
	//数组其他初始化方式
	var arr2 = [5]int{1, 2, 3, 4, 5}
	fmt.Println(arr2)
	//数组内部连续存储，int类型每个数据地址中间间隔8字节
	fmt.Println(&arr2[0], &arr2[1])

	// var arrAge = [5]int{18, 20, 15, 22, 16}
	// var arrLazy = [...]int{5, 6, 7, 8, 22}
	// var arrLazy = []int{5, 6, 7, 8, 22}    //初始化得到的实际上是切片slice
	var arrKeyValue = [5]string{3: "Chris", 4: "Ron"} //将chris放到数组中第4个位置
	// var arrKeyValue = []string{3: "Chris", 4: "Ron"}    //初始化得到的实际上是切片slice
	for i := 0; i < len(arrKeyValue); i++ {
		fmt.Printf("Person at %d is %s\n", i, arrKeyValue[i])
	}

	//使用make生成切片
	var slice1 []int = make([]int, 10)

	for i := 0; i < len(slice1); i++ {
		slice1[i] = 5 * i
	}
	for i := 0; i < len(slice1); i++ {
		fmt.Printf("Slice at %d is %d\n", i, slice1[i])
	}
	fmt.Printf("\n切片长度为 %d\n", len(slice1))
	fmt.Printf("切片容量为 %d\n", cap(slice1))
	items := [...]int{10, 20, 30, 40, 50}
	for i, item := range items {
		items[i] = 2 * item

	}
	fmt.Println(items)

	//切片的复制与添加

	slFrom := []int{1, 2, 3}
	slTo := make([]int, 10)
	n := copy(slTo, slFrom)
	fmt.Println(slTo)
	fmt.Printf("复制 %d 元素\n", n) // n == 3
	sl3 := []int{1, 2, 3}
	sl3 = append(sl3, 4, 5, 6)
	fmt.Println(sl3)

	//map排序

	var (
		m1 = map[int]string{1: "a", 2: "b", 3: "c"}
	)

	for k, v := range m1 {
		fmt.Printf("Key: %v, Value: %v / ", k, v)
	}
	keys := make([]int, len(m1))
	i := 0
	for k, _ := range m1 {
		keys[i] = k
		i++
	}
	sort.Ints(keys)
	for _, k := range keys {
		fmt.Printf("排序后的结果为Key: %v, Value: %v \n ", k, m1[k])
	}
}
